#! /usr/bin/env python

# -*- coding: UTF-8 -*-
# ==============================================================================
# Filename : AboutReader.py
# Purpose  : 
# Author   : Yohann Li (lyh001686@sina.com)
# Date     : 2020-09-02 11:00
# ==============================================================================
import sys

from PyQt5.QtCore import Qt, QPoint
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QWidget, QLabel, QGridLayout, QApplication

from conf.configuration import APPNAME, ABOUT, ICON_SIZE, APPPIC


class AboutReader(QWidget):
    def __init__(self, *args, **kwargs):
        super(AboutReader, self).__init__(*args, **kwargs)
        self._init_ui()
        [l.setTextInteractionFlags(Qt.TextBrowserInteraction) for l in self.findChildren(QLabel)]
        self.setWindowFlag(Qt.FramelessWindowHint, True)
        self.setWindowOpacity(0.8)
        self.resize(300, 220)
        self.move(int((QApplication.desktop().screen().width()-self.width())/2),
                  int((QApplication.desktop().screen().height()-self.height())/3))
        self.installEventFilter(self)

    def _init_ui(self):
        icon_ = self._init_logo()
        app_name = QLabel(APPNAME)
        app_name.setStyleSheet(f"font-weight: bold;font-size: 20px")
        about = QLabel(ABOUT)
        lay = QGridLayout(self)

        lay.addWidget(icon_, 1, 1, 2, 1)
        lay.addWidget(app_name, 1, 2, 1, 2)
        lay.addWidget(about, 2, 2, 3, 2)

    def _init_logo(self):
        icon_label = QLabel()
        icon_ = QPixmap(APPPIC)
        icon_ = icon_.scaled(ICON_SIZE)
        icon_label.resize(ICON_SIZE)
        icon_label.setPixmap(icon_)
        return icon_label

    def eventFilter(self, a0, a1) -> bool:
        if a1.type() == 25:  # 在窗口外点击会触发Windowdeactive，并关闭窗口
            self.close()
        return super(AboutReader, self).eventFilter(a0, a1)

    def mousePressEvent(self, event):
        self.currentPos = event.pos()

    def mouseMoveEvent(self, event):
        self.move(QPoint(self.pos() + event.pos() - self.currentPos))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    app.setCursorFlashTime(0)
    t = AboutReader()
    t.show()
    sys.exit(app.exec_())
